package org.example.library_management_system.Service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.library_management_system.Model.BookInfo;
import org.example.library_management_system.Model.PageRequest;
import org.example.library_management_system.Model.PageResult;
import org.example.library_management_system.Service.BookService;
import org.example.library_management_system.enums.BookStatus;
import org.example.library_management_system.mapper.IBookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private IBookInfoMapper iBookInfoMapper;



    @Override
    public Integer insertBook(BookInfo bookInfo) {
        return iBookInfoMapper.insertBookInfo(bookInfo);
    }

//    @Override
//    public List<BookInfo> selectPageBooks(PageRequest pageRequest) {
//
//        return iBookInfoMapper.selectPageBooks(pageRequest.getOffset(), pageRequest.getPageSize());
//
//    }

    @Override
    public PageResult selectBookByPage(PageRequest pageRequest) {
        //返回书列表和总数
        PageResult pageResult = new PageResult();
        List<BookInfo> bookInfos =  iBookInfoMapper.selectPageBooks(pageRequest.getOffset(),pageRequest.getPageSize());
        pageResult.setBookInfos(bookInfos);
        pageResult.setCount(iBookInfoMapper.selectBooksNums());
        pageResult.setPageRequest(pageRequest);

        for(BookInfo bookInfo: bookInfos){
            bookInfo.setStatusZT(BookStatus.setStatusByCode(bookInfo.getStatus()).getStatus());
        }


        return pageResult;
    }


    @Override
    public Integer selectBooksNums(){
        return iBookInfoMapper.selectBooksNums();
    }

    @Override
    public BookInfo selectBookInfoByIdOrName(BookInfo bookInfo) {
        BookInfo bookInfo1 = iBookInfoMapper.selectBookInfoByIdOrName(bookInfo);
        bookInfo1.setStatusZT(BookStatus.setStatusByCode(bookInfo1.getStatus()).getStatus());
        return bookInfo1;
    }

    @Override
    public BookInfo selectBookInfoById(Integer id) {
        BookInfo bookInfo = iBookInfoMapper.selectBookInfoById(id);
        bookInfo.setStatusZT(BookStatus.setStatusByCode(bookInfo.getStatus()).getStatus());
        return bookInfo;
    }

    @Override
    public Integer updateBookInfoById(BookInfo bookInfo) {
        return iBookInfoMapper.updateBookInfoById(bookInfo);
    }

    @Override
    public Integer deleteBookInfoById(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        return iBookInfoMapper.deleteBookInfoById(bookInfo);
    }

    @Override
    public Integer deleteBookInfoByIds(List<Integer> ids) {
        log.info("ids的值：{}",ids);
        return iBookInfoMapper.deleteBookInfoByIds(ids);
    }


}
